#coding:utf-8
#text display module,version 0.201 by winkidney littlekey

import pygame
from pygame.font import Font,SysFont

import define
class MyText(object):
    #传入screen以访问主屏幕
    def __init__(self,screen,text_start_point=define.TEXT_START_POINT,font=define.TEXT_FONT,font_size=define.FONT_SIZE,):
        try:
            self.font = Font(font,font_size)
        except IOError:
            print "bad font file name or i/o error,sys will load default font"
            self.font = SysFont("simsunnsimsun",font_size,False,False)
        self.screen = screen
        self.text_start_point = text_start_point
    def get_text(self,text_content=""):
        #接受原始文字数据
        self.text_content = text_content
        
    #字体显示，一次仅显示一行，通过多次显示来完成显示
    def text_display(self,text_color=(0,0,0)):
        line_height = self.font.get_linesize()
        char_per_line = define.TEXT_SCREEN_SIZE[0]/line_height
        text_length = len(self.text_content)
        #print char_per_line,line_height,len(self.text_content)
        y = self.text_start_point[1]
        range_times = text_length/char_per_line
        #绘制文字背景
        text_bg = pygame.image.load('ui/window.png').convert_alpha()
        self.screen.blit(text_bg,define.TEXT_WINDOW_POINT)
        #绘制文字
        for i in xrange(range_times+1):
            #用切片保证文字正确换行
            txt_in_line = self.text_content[i*char_per_line:(i+1)*char_per_line]
            if  txt_in_line == "": 
                break
            self.screen.blit(self.font.render(txt_in_line,True,text_color),(self.text_start_point[0],y))
            y += line_height
    def selection_display(self,select_order,text_color=(0,0,0)):
        line_height = self.font.get_linesize()
        #这里的select_order从0开始，这个方法的目的在于绘制选项文字，为button类对象提供一个简单的文字绘制方法
        self.screen.blit(self.font.render(self.text_content,True,text_color),(self.text_start_point[0],self.text_start_point[1]+line_height*select_order))
    def display_by_speed(self,display_speed):
        #字体显示速度，用字的数量计，单位“字/0,1秒”
        self.speed = display_speed
        pass
        #for i in xrange(0,txt_length):
         #   temp_txt = text_content[i]
          #  self.font.render(self.text_content,True,text_color)
       # pygame.display.update()
        
        
        
        
